package com.qfedu.openplateform.manager.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qfedu.openplateform.manager.entity.AjaxMessage;
import com.qfedu.openplateform.manager.entity.ApiSystemparam;
import com.qfedu.openplateform.manager.entity.Res;
import com.qfedu.openplateform.manager.entity.TableData;
import com.qfedu.openplateform.manager.service.ApiSystemparamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lileijian
 * @create 2021-12-02 18:37
 */
@RestController
@RequestMapping("/sys/systemparaters")
public class ApiSystemparamController {


    @Autowired
    private ApiSystemparamService apiSystemparamService;

    @RequestMapping("/add")
    public Res addApiSystemparam(ApiSystemparam apiSystemparam){
        try {
            apiSystemparamService.addApiSystemparam(apiSystemparam);
            return Res.ok(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "添加失败");
    }


    @RequestMapping("/del")
    public Res delApiSystemparams(Integer[] ids){
        try {
            apiSystemparamService.delApiSystemparams(ids);
            return Res.ok(true, "删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false, "删除失败");
    }


    @RequestMapping("/update")
    public Res updateApiSystemparam(ApiSystemparam apiSystemparam){
        try {
            apiSystemparamService.updateApiSystemparam(apiSystemparam);
            return Res.ok(true, "修改成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Res.error(false,"修改失败");
    }


    @RequestMapping("/table")
    public Res queryApiSystemparam(
            @RequestParam(value = "pageNum",required = false,defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",required = false,defaultValue = "10")Integer pageSize){
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<ApiSystemparam> apiSystemparamList = apiSystemparamService.queryApiSystemparam();
            PageInfo<ApiSystemparam> pageInfo = new PageInfo<>(apiSystemparamList);
            return Res.ok(pageInfo.getList(),pageInfo.getTotal());
        } catch (Exception e) {
            Res.exception();
        }
        return Res.error("获取失败，请稍后重试。");
    }


}
